package com.cipe.cmrs.controllers.reports;

import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.cipe.cmrs.service.SsrsReportService;
import com.cipe.cmrs.ssrs.requestmodels.ActiveProjectListQuery;
import com.microsoft.schemas.sqlserver._2005._06._30.reporting.reportingservices.ParameterValue;

/**
 * This class is the controller for getting the SSRS pdf reports via webservice
 * @author Shishir Lamichhane
 *
 */
@Controller
public class SsrsReportController {
	
	
	@Autowired
	private SsrsReportService ssrsReportService;
	
	/**
	 * This method gets the active project list set to end within 30 days pdf response.
	 * @param response
	 */
	@RequestMapping("/secure/showActiveProjectsSetToEnd.do")
	public void getActiveProjectsSetToEndPdf(HttpServletResponse response) {
		String reportName = "/ActiveProjectSetToEndin30Days/ActiveProjectSetToEndin30Days";
		ParameterValue[] parameters = new ParameterValue[0];
		getPdfReports(parameters, reportName, response);
	}
	
	/**
	 * This method gets the active reports pdf response based on criteria.
	 * @param command The criteria domain for querying active project list
	 * @param response
	 */
	@RequestMapping("/secure/showActiveProjectsPdf.do")
	public void getActiveProjectsPdf(@ModelAttribute("activeProjectListQuery") ActiveProjectListQuery command, HttpServletResponse response) {
		
		String reportName = "/ActiveProjects/ActiveProjects";
		String empty = "";
		String singleSpace = " ";
	    ParameterValue[] parameters = new ParameterValue[5];
	    parameters[0] = new ParameterValue();
	    parameters[0].setName("reagin");
	    parameters[0].setValue(command.getRegionName().equalsIgnoreCase(empty)?singleSpace:command.getRegionName());
	    parameters[1] = new ParameterValue();
	    parameters[1].setName("status");
	    parameters[1].setValue(command.getStatusName().equalsIgnoreCase(empty)?singleSpace:command.getStatusName());
	  
	    parameters[2] = new ParameterValue();
	    parameters[2].setName("donorName");
	    parameters[2].setValue(command.getDonorName().equalsIgnoreCase(empty)?singleSpace:command.getDonorName());
	  
	    parameters[3] = new ParameterValue();
	    parameters[3].setName("donorawardnumber");
	    parameters[3].setValue(command.getDonorAwardNumber().equalsIgnoreCase(empty)?singleSpace:command.getDonorAwardNumber());
	  
	    parameters[4] = new ParameterValue();
	    parameters[4].setName("ProgramOfficer");
	    parameters[4].setValue(command.getProgramOfficer().equalsIgnoreCase(empty)?singleSpace:command.getProgramOfficer());
	    getPdfReports(parameters, reportName, response);
	}
	
	/**
	 * This method gets the Production-Bank wire transfer report as pdf response.
	 * @param paymentRequestId
	 * @param beneficiaryName
	 * @param response
	 */
	@RequestMapping("/secure/showBankWireTransferPdf.do")
	public void getBankWireTransferPdf(@RequestParam String paymentRequestId, @RequestParam String beneficiaryName, HttpServletResponse response) {
		
		String reportName = "/BankWireTransfer/BankWireTransferReport";
		String id = paymentRequestId;
		String beneficiarytName = beneficiaryName;
		
		ParameterValue[] parameters = new ParameterValue[2];
		parameters[0] = new ParameterValue();
		parameters[0].setName("id");
		parameters[0].setValue(id);
		parameters[1] = new ParameterValue();
		parameters[1].setName("beneficiarytname");
		parameters[1].setValue(beneficiarytName);
		getPdfReports(parameters, reportName, response);
	}
	
	/**
	 * This method gets the Print Payment Request Form report as pdf response.
	 * @param paymentRequestId
	 * @param response
	 */
	@RequestMapping("/secure/printPaymentRequestFormPdf.do")
	public void getPaymentRequestFormPdf(@RequestParam String paymentRequestId, HttpServletResponse response) {
		
		String reportName = "/PrintPaymentRequestForm/PrintPaymentRequestForm";
		String id = paymentRequestId;
		
		ParameterValue[] parameters = new ParameterValue[1];
		parameters[0] = new ParameterValue();
		parameters[0].setName("paymentrqstid");
		parameters[0].setValue(id);
		getPdfReports(parameters, reportName, response);
	}
	
	/**
	 * This method gets the Print Partner Payment Request report as pdf response.
	 * @param paymentRequestId
	 * @param response
	 */
	@RequestMapping("/secure/printPartnerPaymentRequestPdf.do")
	public void getPartnerPaymentRequestPdf(@RequestParam String paymentRequestId, HttpServletResponse response) {
		
		String reportName = "/PrintPartnerPaymentRequest/PrintPartnerPaymentRequest";
		String id = paymentRequestId;
		
		ParameterValue[] parameters = new ParameterValue[1];
		parameters[0] = new ParameterValue();
		parameters[0].setName("paymentrqstid");
		parameters[0].setValue(id);
		getPdfReports(parameters, reportName, response);
	}
	
	/**
	 * This method gets the Print Partner Payment Request report as pdf response.
	 * @param paymentRequestId
	 * @param response
	 */
	@RequestMapping("/secure/printReclassFormPdf.do")
	public void getReclassFormPdf(@RequestParam String paymentRequestId, HttpServletResponse response) {
		
		String reportName = "/PrintReclassForm/PrintReclassForm";
		String id = paymentRequestId;
		
		ParameterValue[] parameters = new ParameterValue[1];
		parameters[0] = new ParameterValue();
		parameters[0].setName("paymentrqstid");
		parameters[0].setValue(id);
		getPdfReports(parameters, reportName, response);
	}
	
	/**
	 * This method gets the Signature Selection All Unpaid report as pdf response.
	 * @param paymentRequestId
	 * @param response
	 */
	@RequestMapping("/secure/signatureSelectionAllUnpaidPdf.do")
	public void getSignatureSelectionAllUnpaidPdf(@RequestParam String paymentRequestId, HttpServletResponse response) {
		
		String reportName = "/SignatureSelectionAllUnpaid/SignatureSelectionAllUnpaid";
		String id = paymentRequestId;
		
		ParameterValue[] parameters = new ParameterValue[1];
		parameters[0] = new ParameterValue();
		parameters[0].setName("paymentrqstid");
		parameters[0].setValue(id);
		getPdfReports(parameters, reportName, response);
	}
	
	/**
	 * Common method for all the reports 
	 * @param parameters
	 * @param reportName
	 * @param response
	 */
	private void getPdfReports(ParameterValue[] parameters, String reportName, HttpServletResponse response){
		try {
		      // get your file as InputStream
		      InputStream is = ssrsReportService.getResponsePDF(parameters, reportName);
		      // copy it to response's OutputStream
//		      IOUtils.copy(is, response.getOutputStream());
//		      response.flushBuffer();
		      response.setContentType("application/pdf");
		      response.setHeader("Content-Disposition", "inline;filename=\"report.pdf\"");
		      response.setHeader("Pragma", "no-cache");
		      response.setHeader("Cache-Control", "no-cache");
		      
		      byte[] pdfImage = IOUtils.toByteArray(is);
		      response.getOutputStream().write(pdfImage);


		    } catch (IOException ex) {
		      throw new RuntimeException("IOError writing file to output stream");
		    }
	}

}
